﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using OnlineSoccerManager.Classes;

namespace OnlineSoccerManager.Helper
{
    public class Translator
    {
        private Dictionary<string, ResourceValue> translations;

        private static Translator instance;
        public static Translator Instance
        {
            get
            {
                if (Translator.instance == null)
                    Translator.instance = new Translator();

                return Translator.instance;
            }
        }

        private Translator()
        {
            try {
                this.translations = new Dictionary<string, ResourceValue>();
                XmlSerializer serializer = new XmlSerializer(typeof(TranslationCollection));
                CultureInfo current = CultureInfo.CurrentCulture;

                string region = current.Name;
                string country = current.Name.Length < 5 ? "" : region.Substring(0, 5);
                string lang = current.Name.Length < 2 ? "" : region.Substring(0, 2);

                TranslationCollection resources;

                using (MemoryStream mem = new MemoryStream()) {
                    StreamWriter writer = new StreamWriter(mem);
                    writer.Write(Resources.Resources.translation);
                    writer.Flush();
                    mem.Position = 0;
                    resources = serializer.Deserialize(mem) as TranslationCollection;
                    writer.Close();
                    mem.Close();
                }

                foreach (Translation res in resources.Translations) {
                    ResourceValue translation = new ResourceValue() { Value = res.Resource };

                    if (res.Translations != null && res.Translations.Count != 0) {
                        ResourceValue regionValue = res.Translations.FirstOrDefault(t => t.Language == region);
                        ResourceValue countryValue = res.Translations.FirstOrDefault(t => t.Language == country);
                        ResourceValue langValue = res.Translations.FirstOrDefault(t => t.Language == lang);

                        if (langValue != null)
                            translation = langValue;

                        if (countryValue != null)
                            translation = countryValue;

                        if (regionValue != null)
                            translation = regionValue;

                    }

                    this.translations.Add(res.Resource, translation);
                }
            }
            catch (Exception e)
            {

            }
        }

        public ResourceValue this[string resource] {
            get {
                if (resource == null)
                    return new ResourceValue() { Value = String.Empty };

                if (this.translations == null)
                    return new ResourceValue() { Value = resource };

                if (this.translations.ContainsKey(resource))
                    return this.translations[resource];

                return new ResourceValue() { Value = resource };
            }
        }

    }
}
